package com.boc.langxiang.ui.activity.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.BindPhoneContract;
import com.boc.langxiang.entity.events.LoginSuccessEvent;
import com.boc.langxiang.presenter.common.BindPhonePresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.util.KeyBoardUtil;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机
 */
public class BindPhoneActivity extends BaseToolBarActivity implements Handler.Callback, BindPhoneContract.View {

    @BindView(R.id.phone)
    EditText mPhone;
    @BindView(R.id.send)
    TextView mSend;
    @BindView(R.id.checkcode)
    EditText mCheckcode;
    @BindView(R.id.confirm)
    TextView mConfirm;
    private Handler mHandler;
    private int sec;
    private boolean mGetCheckFail;
    private BindPhoneContract.Presenter mPresenter;
    private String mLx;
    private String mOpenid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        ButterKnife.bind(this);
        setToolBarTitle("绑定手机号");

        Intent intent = getIntent();
        mLx = intent.getStringExtra("lx");
        mOpenid = intent.getStringExtra("openid");

        mPresenter = new BindPhonePresenter(this);
        mHandler = new Handler(this);
    }

    @OnClick({R.id.send, R.id.confirm})
    public void onClick(View view) {
        String phone = mPhone.getText().toString();
        String smsCode = mCheckcode.getText().toString();
        switch (view.getId()) {
            case R.id.send:
//                String phone = mPhone.getText().toString();
                if (StringUtil.isEmpty(phone)) {
                    T.showShort(mContext, "请输入手机号码");
                    return;
                }
                sec = 59;
                mGetCheckFail = false;
                mSend.setClickable(false);
                mSend.setText(sec + "s");
                mHandler.sendEmptyMessageDelayed(100, 1000);
                mPresenter.sendCode(phone);
                break;
            case R.id.confirm:
//                String phone = mPhone.getText().toString();
                KeyBoardUtil.closeKeybord(this);
                if (StringUtil.isEmpty(phone)) {
                    T.showShort(mContext, "请输入手机号码");
                    return;
                }
                if (StringUtil.isEmpty(smsCode)) {
                    T.showShort(mContext, "请输入短信验证码");
                    return;
                }
                mPresenter.bindPhone(mLx, mOpenid, phone, smsCode);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 100:
                sec--;
                if (mGetCheckFail || sec == 0) {
                    mSend.setText("发送验证码");
                    mSend.setClickable(true);
                    return true;
                }
                mSend.setText(sec + "s");
                mHandler.sendEmptyMessageDelayed(100, 1000);
                return true;
        }
        return false;
    }

    @Override
    public void showLoading() {
        showProgressDialog("请稍等");
    }

    @Override
    public void hideLoading() {
        super.hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        if (code.equals("0022")) {
            T.showShort(mContext, "该号码尚未注册");
            finish();
        } else {
            T.showShort(mContext, msg);
        }
    }

    @Override
    public void loginSuccess() {
        finish();
        EventBus.getDefault().post(new LoginSuccessEvent());
    }

    @Override
    public void getSmsCodeSuccess() {
        T.showShort(mContext, "短信验证码已经发送至您的手机，请注意查收");
    }

}
